package com.bedpotato.service;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import javax.annotation.Resource;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONObject;
import com.bedpotato.constants.ProxyClawerConstants;
import com.bedpotato.http.BasicHttpMethod;
import com.bedpotato.mapper.ProxyMapper;
import com.bedpotato.po.ProxyPO;
import com.bedpotato.thread.CnproxySiteClawerThread;
import com.bedpotato.thread.FreeproxyListsSiteClawerThread;
import com.bedpotato.thread.GetproxySiteClawerThread;
import com.bedpotato.thread.IpcnClawerThread;
import com.bedpotato.thread.LinktoolSiteClawerThread;
import com.bedpotato.thread.SitediggerSiteClawerThread;
import com.bedpotato.thread.SpysSiteClawerThread;

@Service
public class ProxyClawerService {
	Logger logger = Logger.getLogger(ProxyClawerService.class);
	@Resource
	ProxyMapper proxyMapper;

	public void proxyClaw() {
		// 所有需要运行的抓取线程
		List<Runnable> siteClawServiceList = new ArrayList<Runnable>();
		siteClawServiceList.add(ProxyClawerConstants.applicationContext
				.getBean("cnproxySiteClawerThread",
						CnproxySiteClawerThread.class));
		siteClawServiceList.add(ProxyClawerConstants.applicationContext
				.getBean("freeproxyListsSiteClawerThread",
						FreeproxyListsSiteClawerThread.class));
		siteClawServiceList.add(ProxyClawerConstants.applicationContext
				.getBean("getproxySiteClawerThread",
						GetproxySiteClawerThread.class));
		siteClawServiceList.add(ProxyClawerConstants.applicationContext
				.getBean("ipcnClawerThread", IpcnClawerThread.class));
		siteClawServiceList.add(ProxyClawerConstants.applicationContext
				.getBean("linktoolSiteClawerThread",
						LinktoolSiteClawerThread.class));
		siteClawServiceList.add(ProxyClawerConstants.applicationContext
				.getBean("sitediggerSiteClawerThread",
						SitediggerSiteClawerThread.class));
		siteClawServiceList.add(ProxyClawerConstants.applicationContext
				.getBean("spysSiteClawerThread", SpysSiteClawerThread.class));
		// 线程执行
		ExecutorService executorService = Executors
				.newFixedThreadPool(siteClawServiceList.size());
		logger.info("开始进行全部代理网站抓取");
		for (Runnable thread : siteClawServiceList) {
			executorService.execute(thread);
		}
	}

	public void saveProxy(Collection<ProxyPO> proxyColl) {
		for (ProxyPO proxyPO : proxyColl) {
			saveProxy(proxyPO);
		}
	}

	private void saveProxy(ProxyPO proxyPO) {
		int count = proxyMapper.isHaveProxy(proxyPO);
		if (count == 0) {
			getProxyLocation(proxyPO);
			proxyMapper.insertProxy(proxyPO);
		}
	}

	private void getProxyLocation(ProxyPO proxyPO) {
		String baseURL = "http://ip.taobao.com/service/getIpInfo.php?ip=";
		BasicHttpMethod basicHttpMethod = new BasicHttpMethod();
		try {
			String returnStr = basicHttpMethod.get(baseURL + proxyPO.getIp());
			System.out.println(returnStr);
			JSONObject jsonObject = JSONObject.parseObject(returnStr);
			JSONObject jsonObject2 = JSONObject.parseObject(jsonObject
					.getString("data"));
			proxyPO.setCountry(jsonObject2.getString("country"));
			proxyPO.setRegion(jsonObject2.getString("region"));
			proxyPO.setCity(jsonObject2.getString("city"));
		} catch (Exception e) {
			logger.error(e);
		}
	}

	private void SetAllProxyLocation() throws InterruptedException {
		List<ProxyPO> proxyList = proxyMapper.getAllProxyWithoutLocation();
		for (ProxyPO proxyPO : proxyList) {
			getProxyLocation(proxyPO);
			proxyMapper.updateProxy(proxyPO);
			Thread.sleep(100);
		}
	}

	public static void main(String[] args) throws InterruptedException {
		ProxyClawerService proxyClawerService = ProxyClawerConstants.applicationContext
				.getBean("proxyClawerService", ProxyClawerService.class);
		proxyClawerService.SetAllProxyLocation();
	}
}
